package top.iridescent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.iridescent.constant.ResultMsg;
import top.iridescent.db.annotation.DataSource;
import top.iridescent.entity.MhIncome;
import top.iridescent.entity.MhRole;
import top.iridescent.entity.model.res.ChuTuLvModel;
import top.iridescent.entity.model.res.ChuWoLvModel;
import top.iridescent.entity.model.res.MoneyModel;
import top.iridescent.service.impl.MhIncomeServiceImpl;
import top.iridescent.service.impl.MhRoleServiceImpl;
import top.iridescent.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.iridescent.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @email
 * @date 2021-09-24 11:30:48
 */
@RestController
@Api(tags = "MhIncome")
@RequestMapping("mh/mhincome")
@CrossOrigin
//@DataSource("mh_game_local")
public class MhIncomeController  {
    @Autowired
    private MhIncomeServiceImpl service;
    @Autowired
    private MhRoleServiceImpl roleService;

    @ApiOperation(value = "获取所有角色", notes = "")
    @GetMapping(value = "/getRoles", produces = "application/json")
    public Result getRoles() {
        return Result.ok().setData(roleService.getAll());
    }


    @ApiOperation(value = "获取出窝率", notes = "")
    @GetMapping(value = "/getChuWoLv", produces = "application/json")
    public Result getChuWoLv() {
        return service.getChuWoLv();
    }

    @ApiOperation(value = "新增窝", notes = "")
    @PostMapping(value = "/addWo", produces = "application/json")
    public Result addWo(@RequestBody ChuWoLvModel res) {
        if (ObjectUtils.isEmpty(res.getName()) || ObjectUtils.isEmpty(res.getAllCounts()) || ObjectUtils.isEmpty(res.getWoCounts())) {
            return Result.error(ResultMsg.ERROR_INPUT);
        }
        MhIncome param = new MhIncome();
        param.setRemak(res.getName());
        param.setFrequency(Long.parseLong(res.getAllCounts() + ""));
        param.setThingCount(Long.parseLong(res.getWoCounts() + ""));
        param.setType("7");
        return service.saveInfo(param);
    }


    @ApiOperation(value = "获取出图统计", notes = "")
    @GetMapping(value = "/getChuTuLv", produces = "application/json")
    public Result getChuTu() {
        return service.getChuTuLv();
    }

    @ApiOperation(value = "新增打图", notes = "")
    @PostMapping(value = "/addTu", produces = "application/json")
    public Result addTu(@RequestBody ChuTuLvModel res) {
        if (ObjectUtils.isEmpty(res.getRoleId()) || ObjectUtils.isEmpty(res.getTimes()) || ObjectUtils.isEmpty(res.getAllCounts()) || ObjectUtils.isEmpty(res.getTuCounts())) {
            return Result.error(ResultMsg.ERROR_INPUT);
        }
        MhRole role=(MhRole)roleService.getById(res.getRoleId()).getData();
        MhIncome param = new MhIncome();
        param.setRoleLevel(role.getLevel());
        param.setRoleId(res.getRoleId());
        param.setFrequency(Long.parseLong(res.getAllCounts() + ""));
        param.setThingCount(Long.parseLong(res.getTuCounts() + ""));
        param.setTimes(res.getTimes());
        param.setRemak(role.getRoleName());
        param.setType("2");
        return service.saveInfo(param);
    }


    @ApiOperation(value = "金钱统计", notes = "")
    @GetMapping(value = "/getMoney", produces = "application/json")
    public Result getMoney() {
        QueryWrapper<MhIncome> queryWrapper=new QueryWrapper<>();
        List<String> ss=new ArrayList<>();
        ss.add("i");
        ss.add("o");
        queryWrapper.lambda().in(MhIncome::getType,ss).orderByDesc(MhIncome::getCreateTime);

        QueryWrapper<MhIncome> o =new QueryWrapper<>();
        o.select("SUM(money) as money");
        o.lambda().eq(MhIncome::getType,"o");
        MhIncome out=service.getBaseMapper().selectOne(o);

        QueryWrapper<MhIncome> i =new QueryWrapper<>();
        i.select("SUM(money) as money");
        i.lambda().eq(MhIncome::getType,"i");
        MhIncome in=service.getBaseMapper().selectOne(i);
        MoneyModel res=new MoneyModel();
        if(!ObjectUtils.isEmpty(in)){
            res.setIn(in.getMoney());
        }
        res.setList(service.list(queryWrapper));
        if (!ObjectUtils.isEmpty(out)){
            res.setOut(out.getMoney());
        }
        return Result.ok().setData(res);
    }

    @ApiOperation(value = "新增金钱", notes = "")
    @PostMapping(value = "/addMoney", produces = "application/json")
    public Result addMoney(@RequestBody MhIncome res) {
        res.setCreateTime(new Date());
        return service.saveInfo(res);
    }


    @ApiOperation(value = "师徒统计", notes = "")
    @GetMapping(value = "/getshitu", produces = "application/json")
    public Result getshitu() {
        List<MhIncome> resList=new ArrayList<>();

        QueryWrapper<MhIncome> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(MhIncome::getType,"4");
        List<MhIncome> list=service.list(queryWrapper);
        Map<String,List<MhIncome>> map=list.stream().collect(Collectors.groupingBy(MhIncome::getRemak));
        map.forEach((key,e)->{
            MhIncome mhIncome =new MhIncome();

            e.forEach(tmp->{
                mhIncome.setRoleId(tmp.getRoleId());
                mhIncome.setRemak(tmp.getRemak());
                mhIncome.setWheel(mhIncome.getWheel()+tmp.getWheel());
                mhIncome.setThingCount(mhIncome.getThingCount()+tmp.getThingCount());
                mhIncome.setTimes(mhIncome.getTimes()+tmp.getTimes());
            });
            resList.add(mhIncome);
        });
        return Result.ok().setData(resList);
    }

    @ApiOperation(value = "新增师徒", notes = "")
    @PostMapping(value = "/addshitu", produces = "application/json")
    public Result addshitu(@RequestBody MhIncome res) {
        MhRole role=(MhRole)roleService.getById(res.getRoleId()).getData();
        res.setRemak(role.getRoleName());
        res.setType("4");
        res.setCreateTime(new Date());
        return service.saveInfo(res);
    }





}
